<?php
class Product_Model_ReceivingItem
{

	private $_table;
	//Initalizing Db Table  
	public function __construct(){
		$this->_table = new Product_Model_DbTable_ReceivingItem();
	}
	
	public function add($row){
		return $this->_table->insert($row);
	}	
	
	
	public function delete($value,$field='ri_id'){
		$where = $this->_table->getAdapter()->quoteInto("{$field} = ?", $value);
		return $this->_table->delete($where);
	}
	
	public function getByReceivingId($id){
		$select = $this->_table->select()->where('ri_id = ?', $id);
		return $this->_table->fetchRow($select);
	}
	
	public function update($row,$id){
		$where = $this->_table->getAdapter()->quoteInto('ri_id = ?', $id);
		return $this->_table->update($row,$where);
	}

	public  function updateAsnPid($row,$cond){
		$where = $this->_table->getAdapter()->quoteInto('product_id = ?', $cond['product_id']);
		$where.= $this->_table->getAdapter()->quoteInto(' and receiving_code = ?', $cond['receiving_code']);
		return $this->_table->update($row, $where);	
	}
	
	public function getByReceivingAsnPid($cond){
		$select = $this->_table->select()->where('product_id = ?', $cond['product_id'])
										->where("receiving_code=?",$cond['receiving_code']);
		return $this->_table->fetchRow($select);
	}
	
    public function getByCondition($condition=array(),$type='*',$page="",$pageSize=""){
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table,$type);
        $select->where("1 =?",1);
		if(isset($condition["receiving_code"]) && $condition['receiving_code'] != ""){
			$select->where("receiving_code=?",$condition['receiving_code']);
		}
		if(isset($condition["status"]) && $condition['status'] != ""){
			$select->where("status=?",$condition['status']);
		}
        if(isset($condition["product_id"]) && $condition['product_id'] != ""){
        			$select->where("product_id=?",$condition['product_id']);
        }
        if('count(*)'==$type){
            return $this->_table->getAdapter()->fetchOne($select);
        }else{
            if($pageSize>0 and $page>0)
            {
                $start =($page-1)*$pageSize;
                $select->limit($pageSize,$start);
            }
			$sql=$select->__toString();
		    return $this->_table->getAdapter()->fetchAll($sql);
        }
	}
	
	public function getByProductId($productId){
		$select = $this->_table->select()->where('product_id = ?', $productId);
		return $this->_table->fetchRow($select);
	}
	
	public function getUnNormal($receivingCode){
		$select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table,"count(*)");
		$select->where('receiving_code = ?', $receivingCode)->where("remark != ''");

		return $this->_table->getAdapter()->fetchOne($select);
	}
	
	/**
	 * 根据指定字段返回一行数据
	 **/
	public function getRowByValue($value,$field){
        $select = $this->_table->getAdapter()->select();
        $table  = $this->_table->info('name');
        $select->from($table,'*');
        $select->where("{$field} = ?",$value);
		return $this->_table->getAdapter()->fetchRow($select);
	}
	
	
	public function getAllByValue($value,$field){
        $select = $this->_table->getAdapter()->select();
        $table  = $this->_table->info('name');
        $select->from($table,'*');
        $select->where("{$field} = ?",$value);
		return $this->_table->getAdapter()->fetchAll($select);
	}
	
}